<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="20" :offset="2">
        <!-- 轮播图 -->
        <el-carousel
          class="home_card"
          height="300px"
          type="card"
          direction="horizontal"
          :initial-index="0"
          :autoplay="true"
          :interval="3000"
          :loop="true"
          trigger="hover"
          indicator-position="outside"
          arrow="hover"
        >
          <el-carousel-item v-for="item in banner" :key="item.menuId">
            <el-image
              class="home_card_image"
              :src="item.product_pic_url"
              fit="fill"
            ></el-image>
          </el-carousel-item>
        </el-carousel>
        <!--  -->
        <p class="home_title">内容精选</p>
        <el-container class="list">
          <!-- <el-row :gutter="20" type="flex" justify="space-between">
            <el-col
              v-for="val in menuList.list"
              :key="val.menuId"
              class="home_list_col"
            >
              <router-link to="">
                <el-image
                  :src="val.product_pic_url"
                  fit="fill"
                  :lazy="true"
                ></el-image>
                <h5>{{ val.title }}</h5>
                <p>{{ val.comments_len }} 评论</p>
                <p class="home_list_name">{{ val.name }}</p>
              </router-link>
            </el-col>
          </el-row> -->
          <menuCard :menuList="menuList" width="19%" margin="0 6px 20px" />
        </el-container>
        <p v-show="loading" class="home_loading">
          <i class="el-icon-loading"></i> 加载中
        </p>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import menuCard from "@/components/menu-card.vue";
import { getBanner, getMenuQuery } from "@/api/index";
export default {
  components: { menuCard },
  data() {
    return {
      banner: [{}],
      menuList: {},
      time: null,
      page: 1,
      loading: true,
    };
  },
  methods: {
    windowScroll() {
      this.time = null;
      window.addEventListener("scroll", () => {
        if (this.time) {
          clearTimeout(this.time);
        }
        this.time = setTimeout(() => {
          const window_height = document.body.scrollHeight; //滚动条总高度
          const scrollTop = document.documentElement.scrollTop; //滚动条距离顶部距离
          const clientHeight = document.documentElement.clientHeight; //页面可视区高度
          console.log(window_height, scrollTop, clientHeight);
          console.log(window_height - 50, scrollTop + clientHeight);
          if (scrollTop + clientHeight >= window_height - 20) {
            menuPush();
          }
        }, 300);
      });
      const menuPush = async () => {
        if (
          this.page < this.menuList.total / this.menuList.page_size &&
          !this.loading
        ) {
          this.loading = true;
          this.page = this.page + 1;
          const list = (await getMenuQuery({ page: this.page })).data.list;
          this.menuList.list = this.menuList.list.concat(list);
          this.loading = false;
        }
      };
    },
  },
  async mounted() {
    this.windowScroll();
    this.banner = (await getBanner()).data.list;
    this.menuList = (await getMenuQuery({ page: this.page })).data;
    this.loading = false;
  },
};
</script>


<style lang="stylus" >

.home_card {
  padding-top: 14px;
}

.home_card_image {
  width: 100%;
  // overflow: hidden;
  height: 100%;
}

.home_title {
  font-size: 18px;
  text-align: center;
  margin: 14px 0 26px;
}

// .home_list {
// .el-row--flex {
// flex-wrap: wrap;
// // justify-content: flex-start;
// }

// .el-col {
// width: 19%;
// background: #fff;
// margin-bottom: 20px;
// padding: 0 0 10px 0 !important;

// img {
// border-radius: 4px;
// }

// h5 {
// line-height: 3em;
// margin-left: 16px;
// font-size: 18px;
// }

// p {
// line-height: 30px;
// margin-left: 16px;
// font-size: 14px;
// }

// .home_list_name {
// color: #e62e2d;
// }
// }
// }
.home_loading {
  text-align: center;
  // width: 100%;
}
</style>